<?php

/**
 * XHTML_OptGroup, xhtml_lib
 * 
 * This file contains the source code for the XHTML_OptGroup class.
 * @author Fernando Jimenez Mocobono <fjmcool@gmail.com>
 * @version 0.1
 * @package xhtml_lib
 */

include_once "XHTML_Common_Element.php";
include_once "XHTML_Option.php";
include_once "XHTML__Select_Content.php";

/**
 * The XHTML_OptGroup class is a handler for the option xhtml-strict tag as a part
 * of a xhtml-strict php-handler implementation.
 */
class XHTML_OptGroup extends XHTML_Common_Element implements XHTML__Select_Content {
    
    // xhtml attributes
        
    /**
     * Disables the option-group when it first loads.
     *
     * @var string
     */
    protected $disabled;
    
    /**
     * Defines the label for the option group.
     *
     * @var string
     */
    protected $label;
        
    /**
     * The inner array of options.
     *
     * @var array
     */
    protected $options;
    
    function __construct(Option $option)
    {
        settype($this->options, "array");
        $this->options[] = $option;
    }
    
    /**
     * Specifies if the the option-group should be disabled when it first loads.
     *
     * @param boolean $disabled
     */
    function setDisabled(boolean $disabled)
    {
        $this->disabled = $disabled;
    }
    
    /**
     * Checks if the option-group is going to be disabled when it first loads.
     *
     * @return boolean
     */
    function isDisabled()
    {
        return $this->disabled;
    }
    
    /**
     * Sets the label for the option group.
     *
     * @param string $label
     */
    function setLabel($label)
    {
        $this->label = $label;
    }
    
    /**
     * Gets the label for the option group.
     *
     * @return string
     */
    function getLabel()
    {
        return $this->label;
    }
    
    /**
     * Sets the XHTML_Option elements array.
     *
     * @param array
     */
    function setOptions($options) {
        $this->options = $options;
    }
    
    /**
     * Gets the XHTML_Option elements array.
     *
     * @return array
     */
    function getOptions()
    {
        return $this->options;
    }
    
    /**
     * Adds the specified option to the option-group.
     *
     * @param Option $option
     */
    function addOption(Option $option)
    {
        $this->options[] = $option;
    }
    
    /**
     * Removes the specified option from the option-group.
     *
     * @param Option $option
     */
    function remove(Option $option)
    {
        if (($res = array_search($option, $this->options)) !== false) {
            unset($this->options[$res]);
        }
    }

    // Overriding the toString method for a proper implementation.
    
    /**
    * Returns the string representation for echoing the class atributes.
    *
    * @return string
    */
    function toString()
    {
        $auxString = "<optgroup".parent::toString();
                
        if ($this->disabled) {
            $auxString = $auxString."disabled=\"disabled\" ";
        }
        
        if (isset($this->label)) {
            $auxString = $auxString."label=\"".$this->label."\" ";
        }
        
        $auxString = $auxString.">\n";
        
        /*
         * Addition of the inner option tags of the optgroup tag to the return 
         * string.
         */
        if (count($this->options) > 0) {
            foreach ($this->options as $option) {
                $auxString = $auxString.$option->toString();
            }
        }
        
        $auxString = $auxString."</opgroup>\n";
        
        return $auxString;
    }
    
}

?>